package com.seal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.seal.comm.utils.Page;
import com.seal.dao.BaseDaoImpl;
import com.seal.dao.IDatadictDao;
import com.seal.model.Datadict;

@Repository("datadictDaoImpl")
public class DatadictDaoImpl extends BaseDaoImpl implements IDatadictDao {

	public String getDataRemark(String datakey, String datavalue)
			throws Exception {
		String hql = new StringBuffer(
				"from Datadict t where t.dataKey = ? and t.dataValue= ?")
				.toString();
		Datadict dict = (Datadict) this.getSessionFactory().getCurrentSession()
				.createQuery(hql).setString(0, datakey).setString(1, datavalue)
				.uniqueResult();
		if (dict != null)
			return dict.getRemark();
		return null;
	}

	public String getDataValue(String datakey) throws Exception {
		String hql = new StringBuffer(
				"from Datadict t where t.dataKey = ? ")
				.toString();
		Datadict dict = (Datadict) this.getSessionFactory().getCurrentSession()
				.createQuery(hql).setString(0, datakey)
				.uniqueResult();
		if (dict != null)
			return dict.getDataValue();
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Datadict> getDatadict(String datakey)
			throws Exception {
		String hql = new StringBuffer(
				" from Datadict where dataKey = ? order by id").toString();
		List<Datadict> list = (List<Datadict>) this.getSession().createQuery(
				hql).setString(0, datakey).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Datadict> getAllDatadict(Page pageObj) throws Exception {
		String hql = new StringBuffer("from Datadict").toString();
		Query query =  this.getSession().createQuery(hql);
		if(null != pageObj){
			query.setFirstResult(pageObj.getFirstResult());
			query.setMaxResults(pageObj.getPageSize());
			pageObj.setTotalNumber(this.getDataDictCount());
		}
		List<Datadict> list=(List<Datadict>)query.list();
		return list;
	}
	
	public int getDataDictCount(){
		Query query=this.getSessionFactory().getCurrentSession().createQuery("select count(*) from Datadict");
		return Integer.parseInt(query.uniqueResult().toString());
	}
	
	public boolean addKeyValue(Datadict data) {
		try {
			this.save(data);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.seal.jpa.datadict.IDatadictDao#delDatadict(java.lang.Long)
	 */
	public boolean delDatadict(Long id) {
		this.getSessionFactory().getCurrentSession().createQuery("delete Datadict d where d.id=?")
			.setLong(0, id).executeUpdate();
		return true;
//		Datadict dict=getDatadictById(id);
//		if(dict!=null){
//			try {
//				this.getSessionFactory().getCurrentSession().delete(dict);
//			} catch (Exception e) {
//				e.printStackTrace();
//				return false;
//			}
//			return true;
//		}else{
//			return false;
//		}
	}

	public Datadict getDatadictById(Long id) {
		Datadict dict=(Datadict)this.getSessionFactory().getCurrentSession()
			.createQuery("from Datadict d where d.id=?").setLong(0, id).uniqueResult();
		if(dict!=null)return dict;
		return null;
	}

	public boolean updateKeyValue(Datadict data) {
		this.getSessionFactory().getCurrentSession().update(data);
		return false;
	}
}
